package util

import (
	"path/filepath"

	"github.com/88250/gulu"
)

// 将sourcePath目录打包,放到destinationdir目录下面
//  sourcePath = /home/contract01  ;
// destinationdir = /upload
// 函数执行成功 /upload/contract01.zip
func CompressZip(sourcePath, destinationdir string) (string, error) {
	base := filepath.Base(sourcePath)
	zipFilePath := filepath.Join(destinationdir, base+".zip")
	zipF, zipFErr := gulu.Zip.Create(zipFilePath)
	if zipFErr != nil {
		log.Errorf("CompressZip create [%s] error [%s]",
			zipFilePath, zipFErr.Error())
		return zipFilePath, zipFErr
	}
	defer zipF.Close()
	walkErr := zipF.AddDirectory(base, sourcePath)
	if walkErr != nil {
		log.Errorf("CompressZip Walk [%s] error [%s]", sourcePath, walkErr.Error())
	}
	return zipFilePath, walkErr
}
